﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 渐变
{
    public partial class Notice : Form
    {
        public Notice()
        {
            InitializeComponent();
        }

        public void loadNotice()
        {
            string sql = "select * from notice";
            DataTable dt = DBHelper.ExecuteQuery(sql);
            //设置是否允许自动生成列
            noticedata.AutoGenerateColumns = false;
            noticedata.DataSource = dt;
        }
        public string Value { get; set; }
        public string NameValue { get; set; }
        private void Notice_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            loadNotice();

        }
       

        private void btnAdd_Click(object sender, EventArgs e)
        {
            NoticeForm noticeForm = new NoticeForm();
            noticeForm.opername = this.NameValue;
            DialogResult dr = noticeForm.ShowDialog();
           
           
            if (dr == DialogResult.OK)
            {
                loadNotice();
            }
            //noticeForm.Show();
        }

        private void noticedata_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //双击打开修改窗口
            NoticeForm notice = new NoticeForm();
            notice.opername = this.NameValue;
            notice.id = noticedata.SelectedRows[0].Cells[0].Value.ToString();
            notice.title = noticedata.SelectedRows[0].Cells[1].Value.ToString();
            notice.content = noticedata.SelectedRows[0].Cells[2].Value.ToString();
            DialogResult dr = notice.ShowDialog();
            if (dr == DialogResult.OK)
            {
                loadNotice();
            }

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            DialogResult RSS = MessageBox.Show(this, "确定要删除选中行数据码？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            switch (RSS)
            {
                case DialogResult.Yes:
                    for (int i = this.noticedata.SelectedRows.Count; i > 0; i--)
                    {
                        int NId = Convert.ToInt32(noticedata.SelectedRows[i - 1].Cells[0].Value);
                        noticedata.Rows.RemoveAt(noticedata.SelectedRows[i - 1].Index);
                        //使用获得的ID删除数据库的数据
                        string sql = "delete  from notice where NId=@NId";
                        int s = DBHelper.ExecuteNonQuery(sql, new SqlParameter("@NId", NId));
                        if (s != 0)
                        {
                            MessageBox.Show("成功删除选中行数据！");
                        }
                    }
                    break;
                case DialogResult.No:
                    break;
            }
        }
    }
}
